<?php

namespace app\daren\controller\admin\job;

use app\operations\model\Store;
use sheep\controller\Backend;
use think\exception\ValidateException;
use think\facade\Db;
use think\Request;
use app\daren\model\job\DarenNewJobParticipant as DarenNewJobParticipantModel;
use app\daren\validate\admin\job\NewJob as NewJobValidate;

// 电商视频任务
class CommerceVideoJob extends Backend
{
    public $organ_id;

    public function initialize()
    {
        $this->organ_id = $this->shopId();
        $this->model = new DarenNewJobParticipantModel;
    }

    /**
     * 商品列表
     *
     * @return \think\Response
     */
    public function list(Request $request)
    {
        $user = $this->auth()->user();
        $store_id = $this->shopId();
        $param = $request->param();
        if ($user->role_id != 1) {
            $param['store_id'] = $store_id;
        }
        $daren = $this->model->getList($param);
        return success('获取成功', $daren);
    }

    /**
     * 商品详情
     *
     * @return \think\Response
     */
    public function detail(Request $request, $id)
    {
        $goods = $this->model->detail($id);
        return success('获取成功', $goods);
    }

    // 审核
    public function statusEdit(Request $request)
    {
        $param = $request->only([
            'id', 'status', 'remark'
        ]);
        if (empty($param['id']) || empty($param['status'])) {
            return error('参数有误');
        }
        $user = $this->auth()->user();
        $daren = $this->model->detail($param['id']);
        if ($daren->isEmpty()) {
            return error('该任务不存在');
        }
        if ($daren->status['value'] != 'pending') {
            return error('该实探任务已审核');
        }

        $result = $this->model->dataStatusEdit($param);
        if (!$result) {
            return error('修改失败');
        }

        // 同步萌宠霸王
        $id = $result->external_id;
        unset($param['id']);
//        $param['pass_time']  = time();

        $cutepetDb             = Db::connect('mysql_cutepet');
        $cutepetGroupGoods     = $cutepetDb->name('daren_new_job_participant')->where('id', $id)->update($param);
        if (!$cutepetGroupGoods) {
            return error('同步萌宠霸王失败');
        }

        return success('审核成功');
    }
}